package com.weiaibenpao.demo.chislim.hurui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.weiaibenpao.demo.chislim.R;
import com.weiaibenpao.demo.chislim.bean.UserBean;
import com.weiaibenpao.demo.chislim.hurui.adapte.OffMarathonAdapter;
import com.weiaibenpao.demo.chislim.hurui.bean.OfficialMarathonBean;
import com.weiaibenpao.demo.chislim.model.MyModel;
import com.weiaibenpao.demo.chislim.ui.MarathonWebActivity;
import com.wuxiaolong.pullloadmorerecyclerview.PullLoadMoreRecyclerView;

import java.lang.ref.WeakReference;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.weiaibenpao.demo.chislim.sport.utils.Constant.MARATHON_DETAILURL;
import static com.weiaibenpao.demo.chislim.sport.utils.Constant.MARATHON_ID;
import static com.weiaibenpao.demo.chislim.sport.utils.Constant.MARATHON_TITLE;
import static com.weiaibenpao.demo.chislim.sport.utils.Constant.MARATHON_TYPE;

public class OfficialMarathonActivity extends HBaseActivity {
    private static int PAGE_NUM = 0;
    int num = PAGE_NUM;

    @BindView(R.id.recycler_view_marathon)
    PullLoadMoreRecyclerView recyclerViewMarathon;
    @BindView(R.id.text_title)
    TextView textTitle;
    private OffMarathonAdapter marathonAdapter;
    private Context context = new WeakReference<Activity>(this).get();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_marathon);
        ButterKnife.bind(this);
        textTitle.setText("官方赛事");
        initAdapter();
        initData(0);
    }

    private void initData(int i) {
        MyModel.getModel().getService().marathon(UserBean.getUserBean().userId, i + "", "20").enqueue(new Callback<OfficialMarathonBean>() {
            @Override
            public void onResponse(Call<OfficialMarathonBean> call, Response<OfficialMarathonBean> response) {
                Log.e("wlx33", "onResponse: "+new Gson().toJson(response.body()) );
                if (response.body().getCode() == 0) {
                    marathonAdapter.loadMore(response.body().getData().getMarathonlist());
                }
            }

            @Override
            public void onFailure(Call<OfficialMarathonBean> call, Throwable throwable) {

            }
        });
        recyclerViewMarathon.setPullLoadMoreCompleted();
    }

    private void initAdapter() {
        recyclerViewMarathon.setLinearLayout();
        marathonAdapter = new OffMarathonAdapter(context);
        recyclerViewMarathon.setAdapter(marathonAdapter);
        recyclerViewMarathon.setPullRefreshEnable(false);
        recyclerViewMarathon.setOnPullLoadMoreListener(new PullLoadMoreRecyclerView.PullLoadMoreListener() {
            @Override
            public void onRefresh() {
//                num = PAGE_NUM;
//                initData(0);
            }

            @Override
            public void onLoadMore() {
                num++;
                initData(num);

            }
        });

        marathonAdapter.setOnItemClickListener(new OffMarathonAdapter.OnItemClickListener() {

            public void onItemClickListener(OfficialMarathonBean.DataBean.MarathonlistBean bean) {
//                intent.putExtra(MARATHON_ID,dataBean.getId()+"");
//                intent.putExtra(MARATHON_TITLE,dataBean.getMarathonTitle()+"");
//                intent.putExtra(MARATHON_TYPE,dataBean.getType()+"");
//                intent.putExtra(MARATHON_DETAILURL,dataBean.getDetailUrl());
//                intent.putExtra(MARATHON_TITLE,dataBean.getMarathonTitle());
                Intent intent = new Intent(context, MarathonWebActivity.class);
                intent.putExtra(MARATHON_ID, bean.getId() + "");
                intent.putExtra(MARATHON_TYPE, bean.getType() + "");
                intent.putExtra(MARATHON_DETAILURL, bean.getDetailUrl());
                intent.putExtra(MARATHON_TITLE, bean.getMarathonTitle());

                startActivity(intent);
            }


        });
    }

    @OnClick(R.id.marathon_back)
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.marathon_back:
                finish();
                break;
        }
    }
}
